package dz.chuxin.framework.widget.recycler;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import dz.chuxin.framework.R;

/**
 * RecyclerView 适配器基类
 * Created by chuxin on 2016/6/30.
 */
public abstract class BaseRecyclerAdapter extends RecyclerView.Adapter<BaseViewHolder> {

    public static final int VIEW_TYPE_LOAD_MORE_FOOTER = 100;
    public static final int VIEW_TYPE_NORMAL = 0;

    // "加载更多"底部布局是否显出来
    protected boolean isLoadMoreFooterShown;

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_LOAD_MORE_FOOTER) {
            return onCreateLoadMoreFooterViewHolder(parent);
        }
        return onCreateNormalViewHolder(parent, viewType);
    }

    /**
     * 创建普通的列表项 Item ViewHolder，由子类负责实现
     *
     * @param parent
     * @param viewType
     * @return
     */
    protected abstract BaseViewHolder onCreateNormalViewHolder(ViewGroup parent, int viewType);

    // 创建"加载更多"底部布局ViewHolder
    protected BaseViewHolder onCreateLoadMoreFooterViewHolder(ViewGroup parent) {
        return new LoadMoreFooterViewHolder(
                LayoutInflater.from(parent.getContext()).inflate(getLoadMoreFooterResId(), parent, false));
    }

    /**
     * 默认布局不适合可覆盖此方法自定义实现
     */
    protected int getLoadMoreFooterResId() {
        return R.layout.widget_recycler_refresh_footer;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        // 如果是StaggeredGridLayoutManager，并且是需要显示"加载更多"布局，将"加载更多"跨列显示
        if (isLoadMoreFooterShown && position == getItemCount() - 1) {
            if (holder.itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams) {
                StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams)
                        holder.itemView.getLayoutParams();
                params.setFullSpan(true);
            }
        }
        holder.onBindViewHolder(position);
    }

    @Override
    public int getItemCount() {
        return getDataCount() + (isLoadMoreFooterShown ? 1 : 0);
    }

    @Override
    public int getItemViewType(int position) {
        if (isLoadMoreFooterShown && position == getItemCount() - 1)
            return VIEW_TYPE_LOAD_MORE_FOOTER;
        return getDataViewType(position);
    }

    /**
     * 获取列表项类型
     *
     * @param position
     * @return
     */
    protected int getDataViewType(int position) {
        return VIEW_TYPE_NORMAL;
    }

    // 判断给定位置的ItemView是否为RecyclerView的Footer
    public boolean isLoadMoreFooter(int position) {
        return isLoadMoreFooterShown && position == getItemCount() - 1;
    }

    /**
     * 加载更多有变化时调用，用于通知 RecyclerView 将其显示/隐藏
     *
     * @param isShown "加载更多"提示是否显示
     */
    public void onLoadMoreStateChanged(boolean isShown) {
        this.isLoadMoreFooterShown = isShown;
        if (isShown) {
            notifyItemInserted(getItemCount());
        } else {
            notifyItemRemoved(getItemCount());
        }
    }

    // 是否是列表分组的头部View，支持分组的列表中会设置为true
    public boolean isSectionHeader(int position) {
        return false;
    }

    // 获取数据条数
    protected abstract int getDataCount();

    // 加载更多ViewHolder
    class LoadMoreFooterViewHolder extends BaseViewHolder {
        public LoadMoreFooterViewHolder(View itemView) {
            super(itemView);
        }
    }
}
